package com.wln.sms.sms.config;

import com.alibaba.fastjson.TypeReference;
import com.wln.common.utils.R;
import com.wln.sms.sms.entity.GoodsActivityEntity;
import com.wln.sms.sms.entity.OrderInfoEntity;
import com.wln.sms.sms.feign.GoodsFeignService;
import com.wln.sms.sms.service.GoodsActivityService;
import com.wln.sms.sms.service.OrderInfoService;
import com.wln.sms.sms.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisApplication implements ApplicationRunner {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private GoodsActivityService goodsActivityService;
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private GoodsFeignService goodsFeignService;
    @Override
    public void run(ApplicationArguments args) throws Exception {

        R goodslist = goodsFeignService.list();

        List<GoodsVo> data = goodslist.getData(new TypeReference<List<GoodsVo>>() {
        });
        BoundHashOperations goods = redisTemplate.boundHashOps("goods");

        for (GoodsVo vo : data) {

            goods.put(vo.getGoodsId(),vo.getGoodsStock());
        }

        List<GoodsActivityEntity> list = goodsActivityService.list();

        BoundHashOperations activity = redisTemplate.boundHashOps("activitylist");

        for (GoodsActivityEntity entity : list) {

            activity.put(entity.getActivityId(),entity);
        }

        List<OrderInfoEntity> list1 = orderInfoService.list();

        BoundHashOperations orderinfo = redisTemplate.boundHashOps("orderinfo");

        for (OrderInfoEntity entity : list1) {

            orderinfo.put(entity.getOorderId(),entity);
        }


    }
}
